function SearchData() 
{
	this.nickName = $("#search_input").val();
	this.location = $("#search_location_info").val();
	this.reservest = $("#date_before").val().split("-").join("");
	this.reserveet = $("#date_after").val().split("-").join("");
}


var JoinRoomSearch = function() {
	thisJoinRoomSearch = this;

	this.TAG = " < JoinRoomSearch > ";
	
	/**
	 * ajax 연동 클래스
	 */
	this.cm = null;
	
	/**
	 * 지역 데이터를 받아와 저장하는 변수
	 */
	this.locationList = null;
	
	/**
	 * 초기 전체 리스트 저장 변수
	 */
	this.searchPageList = null;
	
	this.totalPageList = null;
	
	/**
	 * [검색]화면에서 한번에 보여줄 리스트 수
	 */
	this.listCount = 10;
	
	/**
	 * 검색된 데이터에서 접근 위치
	 */
	this.searchAccessPosition = 0;
	
	
	this.pageControl = null;
};

JoinRoomSearch.prototype = {
    constructor : JoinRoomSearch,
    getJoinRoomListData: function()
    {
    	var searchData = new SearchData();

    	console.log(this.TAG + "getJoinRoomListData()");

        var mJson = new MJson();
        mJson.putPub(MJson.PUB.SID, "JR001");
        mJson.putPub(MJson.PUB.SERVICENAME, "getJoinRoomList");
        mJson.putPub(MJson.PUB.LISTCOUNT, this.listCount);
        mJson.putPub(MJson.PUB.CURRPAGENO, this.pageControl.currentPage);
        
        //mJson.putParameter(MJson.PARAMETER.BOOKINGTIME, "");
        mJson.putParameter(MJson.PARAMETER.NICKNAME, searchData.nickName);
        mJson.putParameter(MJson.PARAMETER.LOCATION, searchData.location);
        mJson.putParameter(MJson.PARAMETER.RESERVEST, searchData.reservest);
        mJson.putParameter(MJson.PARAMETER.RESERVEET, searchData.reserveet);
                
        var cm = new ConnectionManager();
        cm.requestGet(mJson.getJsonString(), function(jsonData) {
        
        if(jsonData.msp.contents) 
        {
        	if(jsonData.msp.contents.items) 
        	{
	        	if(jsonData.msp.contents.items.length)
	        	{
		        	for(var i=0; i<jsonData.msp.contents.items.length; i++) 
		        	{
		        		thisJoinRoomSearch.searchPageList.push(jsonData.msp.contents.items[i]);
		        	}
	        	}
	        	else {
	        		thisJoinRoomSearch.searchPageList.push(jsonData.msp.contents.items);
	        	}
	        	
	        	//console.log(thisJoinRoomSearch.TAG + JSON.stringify(jsonData));
	        	//thisJoinRoomSearch.viewList( thisJoinRoomSearch.totalList, jsonData.msp.contents["@totalcount"] );
	        	thisJoinRoomSearch.viewList(jsonData.msp.contents["@totalcount"]);
	        	
        	}
        }
        	
        }, function(e) {
        	alert("error :: " + JSON.stringify(e));
        });
    },
    getJoinRoomLocationListData : function()
    {
    	console.log(this.TAG + "getJoinRoomLocationData()");
    	
        var mJson = new MJson();

        mJson.putPub(MJson.PUB.SID, "JR001");
        mJson.putPub(MJson.PUB.SERVICENAME, "getSelectRegion");
        mJson.putPub(MJson.PUB.LISTCOUNT, "1000");
        mJson.putPub(MJson.PUB.CURRPAGENO, "1");
        
        var cm = new ConnectionManager();
        cm.requestGet(mJson.getJsonString(), function(jsonData) {
        	
        	if(jsonData.msp.contents.items) 
        	{
	        	if(jsonData.msp.contents.items.length)
	        	{
		        	for(var i=0; i<jsonData.msp.contents.items.length; i++) 
		        	{
		        		thisJoinRoomSearch.locationList.push(jsonData.msp.contents.items[i]);
		        	}
	        	}
	        	else {
	        		thisJoinRoomSearch.locationList.push(jsonData.msp.contents.items);
	        	}
	        	
	        	thisJoinRoomSearch.viewLocationList();
        	}
        	
        }, function(e) {
        	alert("error :: " + JSON.stringify(e));
        });
    },
    onPageShow : function() 
    {
    	console.log(this.TAG + "onPageShow()");
    	
    	this.init();
    },
    init: function() 
    {
    	console.log(this.TAG + "init()");
    	
    	this.locationList = new Array(); // 지역리스트
    	this.searchPageList = new Array();	// 검색 리스트
    	this.totalPageList = new Array();
   	
    	this.getJoinRoomLocationListData(); // 지역정보 리스트

    	this.pageControl = new PageControl();
    	
    	$("#search_listview").empty();
    	var add_btn = $("#search_add_btn");
    	add_btn.hide();
    	
    	this.initEvent();
    	
    },
    initEvent: function() 
    {
    	console.log(this.TAG + "initEvent()");
    	
    	$("#search_btn").unbind("click");
    	$("#search_btn").on("click", function($event) {
    		console.log(thisJoinRoomSearch.TAG+"검색버튼!!");
/*    		if(searchData.reservest === "" && searchData.reserveet !== "")
    			return alert("기간을 선택하셔야 합니다.");
    		else if(searchData.reservest !== "" && searchData.reserveet === "");
    			return alert("기간을 선택하셔야 합니다.");
*/
    		$("#search_listview").empty();
    		
    		thisJoinRoomSearch.totalPageList = null;
    		thisJoinRoomSearch.totalPageList = new Array();
    		thisJoinRoomSearch.searchPageList = null;
    		thisJoinRoomSearch.searchPageList = new Array();
    		
    		thisJoinRoomSearch.pageControl.clear();
    		
    		thisJoinRoomSearch.getJoinRoomListData();
    	});
    	
    	$("#search_add_btn").unbind("click");
    	$("#search_add_btn").on("click",function($event){
    		console.log(thisJoinRoomSearch.TAG+"더보기");
    		
        	this.searchPageList = null;
        	this.searchPageList = new Array();
        	
        	thisJoinRoomSearch.pageControl.addView();
        	
    		thisJoinRoomSearch.getJoinRoomListData();
    		
    	});
    	
    	$("#my_join_room_btn").unbind("click");
    	$("#my_join_room_btn").on("click", function($event){
    		console.log(thisJoinRoomSearch.TAG+"내가 만든 조인룸");
    		
    		if (document.globalObj.LOGIN_DATA.isLogin) {
    			$.mobile.changePage("join_room_my.html");
    		} else {
    			
    			var popup = new Popup();
    			popup.confirm("골프인", "로그인 후 이용이 가능합니다. 로그인 하시겠습니까?", "확인, 취소", function($index){
    				if($index == "1") {
    	    			document.globalObj.LOGIN_DATA.loginAfterUrl = '../room/join_room_my.html';
    	    			$.mobile.changePage('../member/login.html');    					
    				}
    			});
/*    			
    			document.globalObj.LOGIN_DATA.loginAfterUrl = '../room/join_room_my.html';
    			$.mobile.changePage('../member/login.html');*/
            }
    	});

    	$("#search_total_join_room_btn").unbind();
    	$("#search_total_join_room_btn").on("click", function($event){
    		console.log(thisJoinRoomSearch.TAG+"전체 조인룸");
    		
    		$.mobile.changePage("join_room.html");
    	});
    },
    initListEvent: function($list, $totalCount) {
/*    	console.log(this.TAG, "initListEvent()");
    	
    	var items = $list;

		$("##search_listview>li").unbind("click");
		$("##search_listview>li").bind("click", function(){
			console.log("아이템 클릭!!!");
			
			var id = $(this).attr("id");
			
			var data = thisJoinRoom.mListData[id];
		
			if(data != undefined) {
				if(data.joinRoomSeq != undefined) {
		    		var joinRoomSeq = data.joinRoomSeq;
		    		var password = data.password;
		
					document.globalObj.joinRoomSeq = joinRoomSeq;
					document.globalObj.joinRoomItemPw = password;
				}

				console.log(data.bookUserId);
				console.log(document.globalObj.LOGIN_DATA.userId);
				
				var changeUrl = null;
				if(document.globalObj.LOGIN_DATA.isLogin) {
					console.log("로그인 on");
					document.globalObj.join_room_pw.afterChangeUrl = data.bookUserId == document.globalObj.LOGIN_DATA.userId ? "join_room_view_my.html":"join_room_view.html";
					changeUrl = data.openYn === "N" ? "join_room_pw.html":data.bookUserId == document.globalObj.LOGIN_DATA.userId ? "join_room_view_my.html":"join_room_view.html";
				} else {
					console.log("로그인 off");
					document.globalObj.LOGIN_DATA.loginAfterUrl = "../room/join_room.html";
					//document.globalObj.join_room_pw.afterChangeUrl = "join_room_view.html";
					changeUrl = "../member/login.html";
					
				}
				
				$.mobile.changePage(changeUrl);
			}
		 });*/
    	
    },
    viewList: function($totalCount)
    {
    	console.log(this.TAG + "viewList()");
    	
    	var concatResult = null;
    	concatResult = this.totalPageList.concat(this.searchPageList);

    	var items = concatResult;
    	
    	console.log("길이:"+items.length);
    	var listview = $("#search_listview");
    	
    	
    	for(var i=0; i<items.length; i++) 
    	{
    		var data = items[i];
    		
    		var sb = new StringBuffer();

    		sb.append("<li id=" + i + ">");
    		sb.append("<a href=# data-rel=dialog data-transition=slide>");
    		sb.append("<ul>");
    		sb.append("<li class='sub'>"+ data.ment + "</li>");
    		sb.append("<li class=date>"+data.reserveDate+"("+data.bookingDay+")"+"</li>");
    		sb.append("<li class=golf_c>"+data.golfClubInfo+"</li>");
    		if(data.openYn === "N") sb.append("<li class=key><span class=ico_all ico1><em>비밀글</em></span></li>");
    		sb.append("<li class=nickname>작성자 :<mark>"+data.nickName+"</mark></li>");
    		sb.append("<li class=reply>댓글<mark>"+"("+(data.replycnt?data.replycnt:"0")+")"+"</mark></li>");
    		sb.append("<li class=state><span class="+(data.procStatus === "C"?"state_ing":data.procStatus === "D"?"state_finish":"state_ok")+">"+data.procStatusNm+"</span></li>");

    		listview.append(sb.toString());
    	}

    	listview.listview("refresh");
    	
    	var add_btn = $("#search_add_btn");
    	add_btn.show();

    	if(concatResult.length >= $totalCount)
    		add_btn.hide();
    	
    	$("#search_listview>li").unbind("click");
    	$("#search_listview>li").bind("click", function() {
    		console.log("아이템 클릭!!!");
    		
    		var id = $(this).attr("id");
    		
    		var data = thisJoinRoom.mListData[id];

    		if(data != undefined) {
    			if(data.joinRoomSeq != undefined) {
    	    		var joinRoomSeq = data.joinRoomSeq;
    	    		var password = data.password;

    				document.globalObj.joinRoomSeq = joinRoomSeq;
    				document.globalObj.joinRoomItemPw = password;
    			}
    			console.log(data.bookUserId);
    			console.log(document.globalObj.LOGIN_DATA.userId);
        		var changeUrl = null;
        		if(document.globalObj.LOGIN_DATA.isLogin) {
        			document.globalObj.join_room_pw.afterChangeUrl = data.bookUserId == document.globalObj.LOGIN_DATA.userId ? "join_room_view_my.html":"join_room_view.html";
        			changeUrl = data.openYn === "N" ? "join_room_pw.html":data.bookUserId == document.globalObj.LOGIN_DATA.userId ? "join_room_view_my.html":"join_room_view.html";
        		} else {
        			
        			document.globalObj.LOGIN_DATA.loginAfterUrl = "../room/join_room_search.html";
        			document.globalObj.join_room_pw.afterChangeUrl = "join_room_view.html";
        			console.log("login false");
        			changeUrl = "../member/login.html";
        			
        		}
        		
        		$.mobile.changePage(changeUrl);
    		}
    	 });
    },
    viewLocationList: function() {
    	console.log(this.TAG + "viewLocationList()");
   	
    	var sb = new StringBuffer();
    	
    	var items = this.locationList;
    	
    	for(var i=0; i<items.length; i++ )
    	{
    		var data = items[i];
    		sb.append("<option value=");
    		sb.append(data.value === undefined ? "" : data.value );
    		sb.append(">");
    		sb.append(data.cname);
    		sb.append("</option>");
    	}

    	$("#search_location_info").append(sb.toString());
    	$("#search_location_info").change();
    	
    	this.getJoinRoomListData();
    }
}

